package com.project.gossip;

import java.util.ArrayList;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.project.gossip.flyoutmenu.FlyOutContainer;
import com.project.gossip.simulation.GossipSimulator;

public class Settings extends ActionBarActivity {

	public RecyclerView mRecyclerView;
	private FlyOutContainer root;
	private String languageToLoad;
	private RadioButton deutsch;
	private RadioButton englisch;
	public static final String PREFS_NAME = "language";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ActionBar actionBar = getSupportActionBar();
		// Creates an arrow that enables the user to quickly move to the parent
		// view
		// --> in this case should be used for all view's despite activity_main
		// to get back to activity_main
		// TODO: needs correct implementation
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setDisplayUseLogoEnabled(true);
		actionBar.setHomeButtonEnabled(true);
		// actionBar.setIcon(R.drawable.launcher_ic);

		this.root = (FlyOutContainer) this.getLayoutInflater().inflate(
				R.layout.settings, null);
		this.setContentView(root);

		SharedPreferences language = getSharedPreferences(PREFS_NAME,
				Context.MODE_PRIVATE);

		// deutsch = (RadioButton) findViewById(R.id.radio_german);
		// englisch = (RadioButton) findViewById(R.id.radio_english);
		languageToLoad = language.getString("lang", "de");
		System.out.println(languageToLoad);
		RadioGroup rg = (RadioGroup) findViewById(R.id.radio_lang);
		rg.clearCheck();
		languageToLoad = languageToLoad.trim();
		if (languageToLoad == "en") {
			System.out.println("en");
			rg.check(R.id.radio_english);
		} else {
			System.out.println("de");
			rg.check(R.id.radio_german);
		}

	}

	public void mainGossip(View v) {
		Intent i = new Intent(this, MainActivity.class);
		startActivity(i);
	}

	public void toggleMenu(View v) {
		this.root.toggleMenu();
	}

	public void startMyGossips(View v) {
		Intent i = new Intent(this, MyGossipsActivity.class);
		startActivity(i);
	}

	public void startNewGossip(View v) {
		Intent i = new Intent(this, NewGossipActivity.class);
		startActivity(i);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {

		case R.id.menu_button:
			toggleMenu(mRecyclerView);
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}

	}

	private void openSearch() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.actionbar_settings, menu);
		return true;
	}

	public void setLanguage(View v) {
		RadioGroup radio = (RadioGroup) findViewById(R.id.radio_lang);
		switch (radio.getCheckedRadioButtonId()) {
		case R.id.radio_german:
			languageToLoad = "de";
			break;
		case R.id.radio_english:
			languageToLoad = "en";
			break;

		default:
			break;
		}
		Locale locale = new Locale(languageToLoad);
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		this.getBaseContext().getResources().updateConfiguration(config, null);
		SharedPreferences language = getSharedPreferences(PREFS_NAME,
				Context.MODE_PRIVATE);

		SharedPreferences.Editor editor = language.edit();
		editor.putString("lang", languageToLoad);
		editor.commit();

		Intent i = new Intent(this, MainActivity.class);
		startActivity(i);
	}
}
